// pages/user/services/service.js
import { Target, HttpUtil } from '../../../utils/http_util.js'
var config = require('../../../utils/config.js')
var utils = require('../../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:'',
    serviceList:[],

    array: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that =this;
    wx.getStorage({
      key: 'userInfo',
      success(res) {
        that.setData({
          userInfo:res.data
        }) 
        HttpUtil.get(Target.self, "artificerMeansType/searchList/"+res.data.id, {}).then(resl => {
          if (resl.status=='0000'){
            that.setData({
              serviceList: resl.data
            })
          }
        })
      }
    })  

    HttpUtil.get(Target.self, "businessTypeBase/list", {}).then(res => {
      that.setData({
        array:res
      });

    })
  },
  bindPickerChange: function (e) {
    var that = this;
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    var relist = that.data.serviceList;
    var checkid = that.data.array[e.detail.value].id
    for(var i=0;i<relist.length;i++ ){
      if (relist[i].id == checkid){
        wx.showToast({
          title: '已添加该项服务',
          icon: 'none',
          duration: 2000
        })
        return false;
      }
    }
    
    HttpUtil.get(Target.self, "artificerMeansType/add", { artId: that.data.userInfo.id, businessTypeId: checkid}).then(res => {
      that.loadlistdata();
    })
  },
  loadlistdata:function(){
    var that =this;
    HttpUtil.get(Target.self, "artificerMeansType/searchList/" + that.data.userInfo.id, {}).then(res => {
      if (res.status == '0000') {
        that.setData({
          serviceList: res.data
        })
      }
    })
  },
  deleteservice:function(event){
    var that =this;
    var id = event.currentTarget.dataset.id;  
    HttpUtil.get(Target.self, "artificerMeansType/deleteArtMenu", { artId: that.data.userInfo.id, businessTypeId: id}).then(res => {
      
      if (res.status == '0000') {
        that.loadlistdata();
      }else{
        wx.showToast({
          title: '系统异常',
          icon: 'none',
          duration: 2000
        })
      }
    })
  }


})